/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.CallableStatement;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import pojo.PhanHePoJo;
import pojo.TaiKhoanPoJo;
import util.DataProvider;

/**
 *
 * @author Nguyen Thi Thu Huong
 */
public class TaiKhoanDao {
    public static TaiKhoanPoJo DangNhap(String tenDN, String matkhau)
    {
        TaiKhoanPoJo tk = null;
        DataProvider dp = new DataProvider();
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(matkhau.getBytes());
            byte[] passBytes = md5.digest();
            
            //String str = String.format("SELECT * FROM taikhoan Where tenDangNhap = BINARY('%s')" +"AND matKhau=BINARY('%s')",tenDN,matkhau);
            String str = String.format("SELECT * FROM taikhoan Where tenDangNhap=? AND matKhau=?");
            
            dp.open();
            PreparedStatement statement = dp.prepareStatement(str);
            statement.setString(1, tenDN);
            statement.setBytes(2, passBytes);
            
            ResultSet rs= dp.executeQuery(str);
            
            if(rs.next())
            {
                tk = new TaiKhoanPoJo();
                tk.setIdTaiKhoan(rs.getInt("idTaiKhoan"));
                tk.setTenDangNhap(rs.getString("tenDangNhap"));
                tk.setMatKhau(rs.getString("matKhau"));
                tk.setEmail(rs.getString("eMail"));
                tk.setPhanhe(null);
            }
            rs.close();
            statement.close();
        }
        catch (SQLException ex)
        {
            //System.out.printf(ex.getMessage());
            ex.printStackTrace();
        }
        catch(NoSuchAlgorithmException ne)
        {
            ne.printStackTrace();
        }
        finally
        {
            dp.close();
        }
        return tk;
    }
    
    public void themTaiKhoan(TaiKhoanPoJo user)
	{
            DataProvider dp = new DataProvider();
            try{
			
                MessageDigest md5 = MessageDigest.getInstance("MD5");
		md5.update(user.getMatKhau().getBytes());
		byte[] passBytes = md5.digest();
                        
                dp.open();
		CallableStatement stm = dp.prepareCall("{CALL sp_TaoTaiKhoan(?,?,?,?)}");
		stm.setString(1, user.getTenDangNhap());
		stm.setBytes(2, passBytes);
		stm.setString(3, user.getEmail());
		stm.setInt(4, user.getPhanhe().getIdPhanhe());
		stm.execute();
		stm.close();
			
            }catch(Exception ex)
            {
		ex.printStackTrace();
            }
            finally
            {
                dp.close();
            }
	}
    
    public List<TaiKhoanPoJo> timTaiKhoan(String tentk, int pagesize, int pageIndex,int[] totalrow)
    {
        DataProvider dp = new DataProvider();
            try{         
                dp.open();
		CallableStatement stm = dp.prepareCall("{CALL sp_TimTaiKhoan(?,?,?,?)}");
		stm.setString(1, tentk);
		stm.setInt(2, pagesize);
		stm.setInt(3, pageIndex);
		stm.execute();
		stm.close();
	
            }catch(Exception ex)
            {
		ex.printStackTrace();
            }
            finally
            {
                dp.close();
            }
        return null;
    }
    
    public TaiKhoanPoJo layTaiKhoanTheoMa(int id)
    {
        DataProvider dp = new DataProvider();
        TaiKhoanPoJo user = null;
        try{
            dp.open();
            String sql = "Select idTaiKhoan,tenDangNhap,matKhau,eMail,idPhanHe from " + 
			" TaiKhoan Where idTaiKhoan=?";
            PreparedStatement stm = dp.prepareStatement(sql);
            stm.setInt(1, id);
            ResultSet rs = stm.executeQuery();
			
            if(rs.next())
            {
                user = new TaiKhoanPoJo();
				
		user.setIdTaiKhoan(rs.getInt(1));
		user.setTenDangNhap(rs.getString(2));
		//user.setMatKhau(rs.getString(3));
		user.setEmail(rs.getString(4));
		user.setPhanhe(new PhanHePoJo(rs.getInt(5)));
				
		rs.close();
            }
			
            stm.close();
	}catch(Exception ex)
	{
            ex.printStackTrace();
            return null;
        }
	finally
	{
            dp.close();
	}
            
        return user;
    }
}
